package cell 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import ru.gosoplata.model.TmpModel;
	
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class KeyPadBtn extends Sprite 
	{
		public static var KEY_PAD_BTN_CLICK:String = "KEY_PAD_BTN_CLICK";
		public static var KEY_PAD_BTN_LOADED:String = "KEY_PAD_BTN_LOADED";
		
		private var index:String;
		private var image_up:Bitmap;
		private var image_down:Bitmap;
		private var icon_image:Bitmap;
		
		public function KeyPadBtn() 
		{
		}
		
		public function init(ind:String):void { 
			index = ind;
			drawFon();
			drawName();
			addEventListener(MouseEvent.CLICK, onPress);
		}
		
		private var color:String;
		
		public function drawFon():void {

			color = (index == "D" || index=="C")?"red":"blue";
			var url:String = TmpModel.IMG_FOLDER+"/vvdod_nomera/images/but_" + color + "_numkeyb.png";
			var req:URLRequest = new URLRequest(url);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplite);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
			loader.load(req);
			var url3:String = TmpModel.IMG_FOLDER+"/vvdod_nomera/images/but_" + color + "_numkeyb_down.png";
			var req3:URLRequest = new URLRequest(url3);
			var loader3:Loader = new Loader();
			loader3.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompliteDown);
			loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
			loader3.load(req3);

		}
		
		private function loadCompliteIcon(e:Event):void 
		{
			icon_image = Bitmap(Loader(e.target.loader).content);
			addChild(icon_image);
			
		}
		
		private function loadComplite(e:Event):void 
		{
			image_up = Bitmap(Loader(e.target.loader).content);
			addChild(image_up);
			swLoadUP = true;
			if (swLoadDOWN) finishInit();
		}
		private var swLoadUP:Boolean = false;
		private var swLoadDOWN:Boolean = false;
		private function finishInit():void {
			dispatchEvent(new Event( KEY_PAD_BTN_LOADED, true));
			if (color == "red") {
				var symb:String = (index == "D")?"icon_arrow.png":"icon_c.png";
				var url2:String = TmpModel.IMG_FOLDER+"/vvdod_nomera/images/"+symb;
				var req2:URLRequest = new URLRequest(url2);
				var loader2:Loader = new Loader();
				loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompliteIcon);
			loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
				loader2.load(req2);
			}else {
				txt.text = index;
				txt.embedFonts = true;
				txt.width = 60;
				txt.height = 60;
				txt.x = 27;
				txt.y = 10;
				txt.selectable = false;
				txt.setTextFormat(TmpModel.getTextFormat(52, 0xFFFFFF,true));
				addChild(txt);
			}		
		}
		
		private	var txt:TextField = new TextField();
		private function loadCompliteDown(e:Event):void {
			image_down = Bitmap(Loader(e.target.loader).content);
			image_down.visible = false;
			addChild(image_down);
			swLoadDOWN = true;
			if (swLoadUP) finishInit();
		}
		
		public var timer:Number;
		private function showDownBtn(e:Event):void 
		{
			timer++;
			if (timer == 5) {
				dispatchEvent(new Event(KEY_PAD_BTN_CLICK, true));
				image_up.visible = true;
				image_down.visible = false;
				if (color == "red") icon_image.alpha = 1;
				else txt.alpha = 1;
				removeEventListener(Event.ENTER_FRAME, showDownBtn);
			}
		}		
		public function drawName():void {
		}
		
		public function onPress(e:Event):void {
			timer = 0;
			image_up.visible = false;
			image_down.visible = true;
			if (color == "red") icon_image.alpha = 0.6;
			else txt.alpha = 0.6;
			addEventListener(Event.ENTER_FRAME, showDownBtn);
		}
		
		public function get indx():String {
			return index;
		}
		
		public function set indx(ind:String):void {
			index = ind;
		}
	}

}